package com.icexxx.parasol;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import springfox.documentation.schema.DefaultGenericTypeNamingStrategy;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ springfox.documentation.spring.web.plugins.Docket.class,
        springfox.documentation.service.ApiInfo.class })
@EnableConfigurationProperties(ConfigInformationProperties.class)
@EnableSwagger2
@Import(IceWebMvcConfigurer.class)
public class ParasolAutoConfiguration {
    @Autowired
    private ConfigInformationProperties configInformationProperties;
    @Autowired(required = false)
    private Environment environment;
    @Autowired(required = false)
    private ServerProperties serverProperties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass(DefaultGenericTypeNamingStrategy.class)
    public Docket createRestApi() {
        ConfigInformationProperties data = ConfigInformationInit.data();
        data = init(data, serverProperties);
        Docket docket = ConfigInformationInit.docket(data, environment);
        String url = data.getUrl();
        String groupName = data.getGroupName();
        if (StringUtils.hasText(url)) {
            docket.host(url);
        }
        if (StringUtils.hasText(groupName)) {
            docket.groupName(groupName);
        }
        return docket;
    }

    protected ConfigInformationProperties init(ConfigInformationProperties configInformation,
            ServerProperties serverProperties) {
        String title = configInformationProperties.getTitle();
        String description = configInformationProperties.getDescription();
        String termsOfServiceUrl = configInformationProperties.getTermsOfServiceUrl();
        String name = configInformationProperties.getName();
        String url = configInformationProperties.getUrl();
        String email = configInformationProperties.getEmail();
        String license = configInformationProperties.getLicense();
        String licenseUrl = configInformationProperties.getLicenseUrl();
        String version = configInformationProperties.getVersion();
        String basePackage = configInformationProperties.getBasePackage();
        String groupName = configInformationProperties.getGroupName();
        String pathSelectors = configInformationProperties.getPathSelectors();
        if (StringUtils.hasText(title)) {
            configInformation.setTitle(title);
        }
        if (StringUtils.hasText(description)) {
            configInformation.setDescription(description);
        }
        if (StringUtils.hasText(termsOfServiceUrl)) {
            configInformation.setTermsOfServiceUrl(termsOfServiceUrl);
        }
        if (StringUtils.hasText(name)) {
            configInformation.setName(name);
        }
        if (StringUtils.hasText(email)) {
            configInformation.setEmail(email);
        }
        if (StringUtils.hasText(license)) {
            configInformation.setLicense(license);
        }
        if (StringUtils.hasText(licenseUrl)) {
            configInformation.setLicenseUrl(licenseUrl);
        }
        if (StringUtils.hasText(version)) {
            configInformation.setVersion(version);
        }
        if (StringUtils.hasText(basePackage)) {
            configInformation.setBasePackage(basePackage);
        }
        if (StringUtils.hasText(groupName)) {
            configInformation.setGroupName(groupName);
        }
        if (StringUtils.hasText(pathSelectors)) {
            configInformation.setPathSelectors(pathSelectors);
        }
        String urlDefault = initUrl(serverProperties);
        if (StringUtils.hasText(urlDefault)) {
            configInformation.setUrl(urlDefault);
        }
        if (StringUtils.hasText(url)) {
            configInformation.setUrl(url);
        }
        return configInformation;
    }

    private String initUrl(ServerProperties serverProperties) {
        Integer port = null;
        if (serverProperties != null) {
            port = serverProperties.getPort();
        }
        String defaultUrl = null;
        InetAddress localHost = null;
        try {
            localHost = Inet4Address.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        if (localHost != null) {
            defaultUrl = localHost.getHostAddress();
        }
        String portDefault = "8080";
        if (port != null) {
            portDefault = String.valueOf(port);
        }
        String result = "http://" + defaultUrl + ":" + portDefault + "/";
        return result;
    }
}
